import React, { useReducer, useState } from "react";
import queryApi from "../../api/query";

const GoodsCheckContext = React.createContext();

const dataReducer = (state, action) => {
  switch (action.type) {
    case "add_data":
      return state;
    case "edit_data":
      // console.log("--value--", action.payload.value);
      // console.log("--value222--", [state[0], action.payload.value]);
      // return action.payload.value;
      return [state[0], action.payload.value];
    case "sort_data":
      let page = action.payload.page;
      let way = action.payload.way;
      let arr = state[1][page];
      let len = arr.length;
      // console.log("arr=================", way);
      for (let i = 0; i < len; i++) {
        for (let j = 0; j < len - 1 - i; j++) {
          if (way == 1) {
            if (parseFloat(arr[j].price) > parseFloat(arr[j + 1].price)) {
              let temp = arr[j + 1];
              arr[j + 1] = arr[j];
              arr[j] = temp;
            }
          } else if (way == 0) {
            if (parseFloat(arr[j].price) < parseFloat(arr[j + 1].price)) {
              let temp = arr[j + 1];
              arr[j + 1] = arr[j];
              arr[j] = temp;
            }
          }
        }
      }
      // console.log("排序后-----", arr);
      state[1][page] = arr;
      return state;
    default:
      return state;
  }
};

let initData = [
  { priceSort: 0, place: "全国", type: {} },
  [
    [],
    //   {
    //     basePlate: {
    //       name: "钢7070",
    //       density: 1.41,
    //       unit_price: [1200, "元/㎡"],
    //       pro_price: [1150, "元/㎡"],
    //       res_price: 35714.36000000001,
    //     },
    //     shroudPlate: {
    //       name: "铝Q780",
    //       density: 1.68,
    //       unit_price: [1500, "元/kg"],
    //       pro_price: [1150, "元/kg"],
    //       res_price: 130824960,
    //     },
    //     company: {
    //       company_name: "刘盼盼文化有限公司",
    //       address: "陕西西安",
    //       detailed_address: "陕师大",
    //       admin_name: "刘盼盼",
    //       admin_phone: "12345678901",
    //     },
    //     price: 130860674.36,
    //     amount: 2,
    //   },
    // {
    //   basePlate: {
    //     name: "不锈钢板",
    //     density: [1.68, 0],
    //     price: 2000,
    //     processCharge: 500,
    //   },
    //   shroudPlate: {
    //     name: "7075铝板",
    //     density: [1.68, 0],
    //     price: 4000,
    //     processCharge: 800,
    //   },
    //   unitPrice: [26350, 0],
    // },
  ],
];

export const GoodsCheckProvider = ({ children }) => {
  const [isLoading, setIsLoading] = useState(true);

  const [queryData, dispatch] = useReducer(dataReducer, initData);
  // console.log("initQueryData---", queryData);

  const addData = () => {
    dispatch({ type: "add_data" });
  };

  const editData = (value) => {
    // console.log("编辑前------", value);
    setIsLoading(true);
    dispatch({
      type: "edit_data",
      payload: { value: value },
    });
    setIsLoading(false);
    // console.log("vvvvv----?", queryData);
  };

  const selectPrice = async (quoData) => {
    // console.log(quoData);
    // console.log("进入");
    setIsLoading(true);
    try {
      // console.log(quoData);
      // console.log("开始查");
      const queryData = await queryApi.post("/goods/cal", quoData);
      // console.log("========111=====================", queryData.data.data);
      dispatch({
        type: "edit_data",
        payload: { value: queryData.data.data },
      });
      setIsLoading(false);
    } catch (error) {
      setIsLoading(false);
    }
  };

  const sortPrice = (page, way) => {
    console.log("排序");
    dispatch({
      type: "sort_data",
      payload: { page: page, way: way },
    });
  };

  return (
    <GoodsCheckContext.Provider
      value={{
        isLoading,
        queryData: queryData,
        addData,
        editData,
        selectPrice,
        sortPrice,
      }}
    >
      {children}
    </GoodsCheckContext.Provider>
  );
};

export default GoodsCheckContext;
